package com.team.teamproject.ui.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.team.teamproject.R;
import com.team.teamproject.bean.DetailsData;
import com.team.teamproject.utils.HttpUtils;
import com.team.teamproject.utils.ParseJson;

import java.util.List;

public class HomePage_XiangQingActivity extends AppCompatActivity {
  String url="http://api.liwushuo.com/v2/posts_v2/";
    String id;
    String xqUrl;
    ImageView iv;
    WebView webView;
    Handler handler = new Handler();
    //List<DetailsData> detailsData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_homepage__xiangqing);
        iv= (ImageView) findViewById(R.id.iv);
        //获取传过来的id
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        Log.i("aaa","详情页获取到的Id--->"+id);
        xqUrl=url+id;
        //获取数据
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
        toolbar.setNavigationIcon(R.mipmap.title_bar_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        webView = (WebView) findViewById(R.id.webview);
//        //得到WebView的设置器
//        WebSettings settings = webView.getSettings();
//        //是的Javascript功能可用
//        settings.setJavaScriptEnabled(true);
//        //设置网页的编码格式
//        settings.setDefaultTextEncodingName("UTF-8");
//        //显示一个宽的视图窗口
//        settings.setUseWideViewPort(true);
//        //加载俯视模式的窗口
//        settings.setLoadWithOverviewMode(true);
//        webView.loadUrl(xqUrl);
        getData();
        //detailsData = new ArrayList<>();

    }

    public void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = HttpUtils.getHttpUtils().getJson(xqUrl);
                if (json!=null){
                    final List<DetailsData> list = ParseJson.fromToHomeXq(json);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            DetailsData detailsData = list.get(0);
                            webView.loadDataWithBaseURL(null,detailsData.getContent_html(),"text/html","utf-8",null);
                            WebSettings webSettings = webView.getSettings();
                            webSettings.setDefaultTextEncodingName("utf-8");
                            webView.setWebViewClient(new WebViewClient(){
                                @Override
                                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                    return false;
                                }
                            });
                        }
                    });
                }
            }
        }).start();
    }
}
